import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios';
import createPersistedState from "vuex-persistedstate";
import logger from 'vuex/dist/logger'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    AllList:[],
  },
  mutations: {
    renderList(state,payload){
      state.AllList = payload;
    },
    // 删除
    DelBtn(state,payload){
      let arr = [...state.AllList]
      arr.forEach((item,index)=>{
        if(item.id == payload){
          arr.splice(index,1)
        }
      })
      state.AllList = arr
    },
    // 新增
    AddBtn(state,payload){
      let arr = [...state.AllList]
      arr.unshift(payload);
      state.AllList = arr
    },
    // 编辑
    EditBtn(state,payload){
      let arr = [...state.AllList]
      arr.forEach((item,index)=>{
        if(item.id == payload.id){
          Object.assign(item,payload);
        }
      })
      state.AllList = arr
    },
  },
  actions: {
    asyncList(){
      axios.get('/api/list').then(res=>{
        this.commit('renderList',res.data);
        // console.log(res.data);
      })
    }
  },
  modules: {
  },
  plugins: [logger(),createPersistedState({
    storage:window.sessionStorage
  })],
})
